import os
from pkgr.package import RemotePackage
from pkgr.package import Requires
from pkgr.package import Provides
from pkgr.package import Obsoletes

class RepoPackage(RemotePackage):
    """ A package from the repository database. 
    
        .. inheritance-diagram:: pkgr.repository.package.RepoPackage    
    """
    
    _obsoletes = None
    _provides = None
    _requires = None
    _files = None
    
    def __init__(self, repo, db, **kwargs):
        super(RepoPackage, self).__init__(repo, **kwargs)
        self.db = db                                                                                                                                 
        self.location_href = kwargs.get('location_href')
        self.pkgKey = kwargs.get('pkgKey')
        self.description = kwargs.get('description')
        self.summary = kwargs.get('summary')
        
    def get_description(self):
        return self.description
     
    def get_summary(self):
        return self.summary
        
    def get_dbpkgkey(self):
        return self.pkgKey
        
    def get_location(self):
        return self.location_href
        
    def get_basename(self):
        return os.path.basename(self.get_location())
        
    def get_download_url(self):
        return self.get_repository().get_current_mirror()+'/'+self.get_location()
        
    def get_all_provides(self):
        for p in self.get_provides():
            yield p.get_name()
            
        for p in self.get_files():
            yield p.get_name()
       
    def get_obsoletes(self):
        if self._obsoletes is None:
            db_curs = self.db.cursor()
            db_curs.execute("SELECT name, flags, epoch, version, release FROM obsoletes WHERE pkgKey = ?;", (self.get_dbpkgkey(),))
            self._obsoletes = set()
            for obsolete in db_curs.fetchall(): 
                self._obsoletes.add(Obsoletes(**obsolete))
            self._obsoletes = self._obsoletes
            
        return self._obsoletes
     
    def get_files(self):
        if self._files is None:
            db_curs = self.db.cursor()
            db_curs.execute("SELECT name FROM files WHERE pkgKey = ?;", (self.get_dbpkgkey(),))
            self._files = set()
            for file in db_curs.fetchall(): 
                self._files.add(Provides(file['name']))
            self._files = self._files
            
        return self._files
        
    def get_provides(self):
        if self._provides is None:
            db_curs = self.db.cursor()
            db_curs.execute("SELECT name, flags, epoch, version, release FROM provides WHERE pkgKey = ?;", (self.get_dbpkgkey(),))
            self._provides = set()
            for provide in db_curs.fetchall(): 
                self._provides.add(Provides(**provide))
            self._provides = self._provides
            
        return self._provides
    
    def get_requires(self):
        if self._requires is None:
            db_curs = self.db.cursor()
            db_curs.execute("SELECT name, flags, epoch, version, release FROM requires WHERE pkgKey = ?;", (self.get_dbpkgkey(),))
            self._requires = set()
            
            for require in db_curs.fetchall(): 
                self._requires.add(Requires(**require))
            self._requires = self._requires
        return self._requires
    
    def __str__(self):
        return "%s-%s-%s:%s.%s" % (
            self.get_name(),
            self.get_version(), 
            self.get_release(), 
            self.get_epoch(),
            self.get_arch()
        )
        
    def __repr__(self):
        return "<RepoPackage(%s-%s-%s:%s.%s, %s)>" % (
            self.get_name(),
            self.get_version(), 
            self.get_release(), 
            self.get_epoch(),
            self.get_arch(),
            self.get_repository().get_name()
        )
